package main

import (
	grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
	grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
	"log"
	global2 "longmen/server/config/global"
	middleware2 "longmen/server/internal/rpc/middleware"
	"longmen/server/internal/rpc/payment/server"
	"longmen/server/pkg/common/app"
	"longmen/server/pkg/common/app/etcd"
	appServer "longmen/server/pkg/common/app/server"
	"longmen/server/pkg/common/helper/const"
	pb "longmen/server/pkg/protobuf"
)

func main() {
	global2.Initialize("../../../config/app.properties")

	limit := middleware2.NewPaymentRequestLimit()
	//metrics := prometheus.NewGrpcMetrics()
	grpcSrv := appServer.New(
		appServer.Network("tcp"),
		appServer.Address(":"+global2.Config.GetString("grpc.payment_server_port")),
		appServer.UnaryServerInterceptor(
			grpcrecovery.UnaryServerInterceptor(appServer.RecoveryOption()...),
			middleware2.TokenValid,
			limit.RequestLimit,
			grpczap.UnaryServerInterceptor(global2.ZapLogger),
			//metrics.GrpcMetrics.UnaryServerInterceptor(),
		),
	)
	grpcPaymentServer := &server.Server{}
	pb.RegisterPaymentServerServer(grpcSrv, grpcPaymentServer)
	app := app.NewApp(
		app.Version(global2.GetMicroserviceVersion()),
		app.Name(global2.GetMicroserviceName(_const.GRPC_PAYMENT_SERVER_NAME_KEY)),
		app.Register(etcd.NewEtcdRegister(global2.GetEtcdCli())),
		app.Server(grpcSrv),
	)
	//metrics.StarHttpServer(grpcSrv, global2.Config.GetInt("prometheus.payment.port"))
	//pyroscope.InitPyroScope("payment")
	if err := app.Start(); err != nil {
		log.Fatalf("init account server is error: %+v \n", err)
	}
}
